package com.nowcoder.community.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: shuhuang
 * @description:
 * @Date: 2022/10/5 9:22
 * @Version java version 1.8
 */
@Component
public class TestInterceptor implements HandlerInterceptor {
    private static final Logger logger = LoggerFactory.getLogger(TestInterceptor.class);

    /*************************************************************
     * 1、只要有一个拦截器preHandle不放行，则所有拦截器的postHandle()都不执行
     * 2、返回false的拦截器之前的拦截器的afterComplation()都会执行
     *************************************************************/

    /**
     * 在控制器方法之前先执行的
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.debug("preHandle:"+handler.toString());
        return true;
    }

    /**
     * 在控制器方法之后先执行的
     * 这个方法中你可以对ModelAndView进行操作。
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        logger.debug("postHandle:我被执行了");
    }

    /**
     * 在TemplateEngine（视图渲染完毕）之后执行
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        logger.debug("afterCompletion:我被执行了");
    }
}
